home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / radio / ulawadpcm.c < prev   
C/C++ Source or Header  |  1994-08-01  |  7KB  |  278 lines

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
  3. Amsterdam, The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /*
  26. ** Intel/DVI ADPCM coder/decoder.
  27. **
  28. ** The algorithm for this coder was taken from the IMA Compatability Project
  29. ** proceedings, Vol 2, Number 2; May 1992.
  30. **
  31. ** Version 1.1, 16-Dec-92.
  32. **
  33. ** Change log:
  34. ** - Fixed a stupid bug, where the delta was computed as
  35. **   stepsize*code/4 in stead of stepsize*(code+0.5)/4. The old behavior can
  36. **   still be gotten by defining STUPID_V1_BUG.
  37. */
  38.  
  39. #include "adpcm.h"
  40. #include "libst.h"
  41.  
  42. #ifndef __STDC__
  43. #define signed
  44. #endif
  45.  
  46. /* Intel ADPCM step variation table */
  47. static int indexTable[16] = {
  48.     -1, -1, -1, -1, 2, 4, 6, 8,
  49.     -1, -1, -1, -1, 2, 4, 6, 8,
  50. };
  51.  
  52. static int stepsizeTable[89] = {
  53.     7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  54.     19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  55.     50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  56.     130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  57.     337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  58.     876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  59.     2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  60.     5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  61.     15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  62. };
  63.     
  64. void
  65. ulaw_adpcm_coder(indata, outdata, len, state)
  66.     char indata[];
  67.     char outdata[];
  68.     int len;
  69.     struct adpcm_state *state;
  70. {
  71.     unsigned char *inp;        /* Input buffer pointer */
  72.     signed char *outp;        /* output buffer pointer */
  73.     /* XXX why must outp be signed? */
  74.     int val;            /* Current input sample value */
  75.     int sign;            /* Current adpcm sign bit */
  76.     int delta;            /* Current adpcm output value */
  77.     int step;            /* Stepsize */
  78.     int valprev;        /* virtual previous output value */
  79.     int vpdiff;            /* Current change to valprev */
  80.     int index;            /* Current step change index */
  81.     int outputbuffer;        /* place to keep previous 4-bit value */
  82.     int bufferstep;        /* toggle between outputbuffer/output */
  83.  
  84.     outp = (signed char *)outdata;
  85.     inp = (unsigned char *)indata;
  86.  
  87.     if (state) {
  88.     valprev = state->valprev;
  89.     index = state->index;
  90.     }
  91.     else {
  92.     valprev = 0;
  93.     index = 0;
  94.     }
  95.     step = stepsizeTable[index];
  96.     
  97.     bufferstep = 1;
  98.  
  99.     for ( ; len > 0 ; len-- ) {
  100.     val = st_ulaw_to_linear(*inp++);
  101.  
  102.     /* Step 1 - compute difference with previous value */
  103.     delta = val - valprev;
  104.     sign = (delta < 0) ? 8 : 0;
  105.     if ( sign ) delta = (-delta);
  106.  
  107.     /* Step 2 - Divide and clamp */
  108. #ifdef NODIVMUL
  109.         {
  110.         int tmp = 0;
  111. #ifdef STUPID_V1_BUG
  112.         vpdiff = 0;
  113. #else
  114.         vpdiff = (step >> 3);
  115. #endif /* STUPID_V1_BUG */
  116.         if ( delta > step ) {
  117.         tmp = 4;
  118.         delta -= step;
  119.         vpdiff += step;
  120.         }
  121.         step >>= 1;
  122.         if ( delta > step  ) {
  123.         tmp |= 2;
  124.         delta -= step;
  125.         vpdiff += step;
  126.         }
  127.         step >>= 1;
  128.         if ( delta > step ) {
  129.         tmp |= 1;
  130.         vpdiff += step;
  131.         }
  132.         delta = tmp;
  133.     }
  134. #else
  135.     delta = (delta<<2) / step;
  136.     if ( delta > 7 ) delta = 7;
  137.  
  138. #ifdef STUPID_V1_BUG
  139.     vpdiff = (delta*step) >> 2;
  140. #else
  141.     vpdiff = ((delta*step) >> 2) + (step >> 3);
  142. #endif /* STUPID_V1_BUG */
  143. #endif /* NODIVMUL */
  144.       
  145.     /* Step 3 - Update previous value */
  146.     if ( sign )
  147.       valprev -= vpdiff;
  148.     else
  149.       valprev += vpdiff;
  150.  
  151.     /* Step 4 - Clamp previous value to 16 bits */
  152.     if ( valprev > 32767 )
  153.       valprev = 32767;
  154.     else if ( valprev < -32768 )
  155.       valprev = -32768;
  156.  
  157.     /* Step 5 - Assemble value, update index and step values */
  158.     delta |= sign;
  159.     
  160.     index += indexTable[delta];
  161.     if ( index < 0 ) index = 0;
  162.     if ( index > 88 ) index = 88;
  163.     step = stepsizeTable[index];
  164.  
  165.     /* Step 6 - Output value */
  166.     if ( bufferstep ) {
  167.         outputbuffer = (delta << 4) & 0xf0;
  168.     } else {
  169.         *outp++ = (delta & 0x0f) | outputbuffer;
  170.     }
  171.     bufferstep = !bufferstep;
  172.     }
  173.  
  174.     /* Output last step, if needed */
  175.     if ( !bufferstep )
  176.       *outp++ = outputbuffer;
  177.  
  178.     if (state) {
  179.     state->valprev = valprev;
  180.     state->index = index;
  181.     }
  182. }
  183.  
  184. void
  185. adpcm_ulaw_decoder(indata, outdata, len, state)
  186.     char indata[];
  187.     char outdata[];
  188.     int len;
  189.     struct adpcm_state *state;
  190. {
  191.     signed char *inp;        /* Input buffer pointer */
  192.     char *outp;            /* output buffer pointer */
  193.     int sign;            /* Current adpcm sign bit */
  194.     int delta;            /* Current adpcm output value */
  195.     int step;            /* Stepsize */
  196.     int valprev;        /* virtual previous output value */
  197.     int vpdiff;            /* Current change to valprev */
  198.     int index;            /* Current step change index */
  199.     int inputbuffer;        /* place to keep next 4-bit value */
  200.     int bufferstep;        /* toggle between inputbuffer/input */
  201.  
  202.     outp = outdata;
  203.     inp = (signed char *)indata;
  204.  
  205.     if (state) {
  206.     valprev = state->valprev;
  207.     index = state->index;
  208.     }
  209.     else {
  210.     valprev = 0;
  211.     index = 0;
  212.     }
  213.     step = stepsizeTable[index];
  214.  
  215.     bufferstep = 0;
  216.     
  217.     for ( ; len > 0 ; len-- ) {
  218.     
  219.     /* Step 1 - get the delta value and compute next index */
  220.     if ( bufferstep ) {
  221.         delta = inputbuffer & 0xf;
  222.     } else {
  223.         inputbuffer = *inp++;
  224.         delta = (inputbuffer >> 4) & 0xf;
  225.     }
  226.     bufferstep = !bufferstep;
  227.  
  228.     /* Step 2 - Find new index value (for later) */
  229.     index += indexTable[delta];
  230.     if ( index < 0 ) index = 0;
  231.     if ( index > 88 ) index = 88;
  232.  
  233.     /* Step 3 - Separate sign and magnitude */
  234.     sign = delta & 8;
  235.     delta = delta & 7;
  236.  
  237.     /* Step 4 - update output value */
  238. #ifdef NODIVMUL
  239. #ifdef STUPID_V1_BUG
  240.     vpdiff = 0;
  241. #else
  242.     vpdiff = step >> 1;
  243. #endif /* STUPID_V1_BUG */
  244.     if ( delta & 4 ) vpdiff += (step << 2);
  245.     if ( delta & 2 ) vpdiff += (step << 1);
  246.     if ( delta & 1 ) vpdiff += step;
  247.     vpdiff >>= 2;
  248. #else
  249. #ifdef STUPID_V1_BUG
  250.     vpdiff = ((delta*step) >> 2);
  251. #else
  252.     vpdiff = ((delta*step) >> 2) + (step >> 3);
  253. #endif /* STUPID_V1_BUG */
  254. #endif /* ! NODIVMUL */
  255.     if ( sign )
  256.       valprev -= vpdiff;
  257.     else
  258.       valprev += vpdiff;
  259.  
  260.     /* Step 5 - clamp output value */
  261.     if ( valprev > 32767 )
  262.       valprev = 32767;
  263.     else if ( valprev < -32768 )
  264.       valprev = -32768;
  265.  
  266.     /* Step 6 - Update step value */
  267.     step = stepsizeTable[index];
  268.  
  269.     /* Step 7 - Output value */
  270.     *outp++ = st_linear_to_ulaw(valprev);
  271.     }
  272.  
  273.     if (state) {
  274.     state->valprev = valprev;
  275.     state->index = index;
  276.     }
  277. }
  278.